<?php
namespace Bshop\Controller;
class AccountController extends CkadminController {
	/**
	 * 商家基础信息
	 */
    public function index(){
    	$uid = session('bxd_shop_user_id');
    	$res = $this->admin_name($uid);
    	if(!empty($_POST)){
    		$res = $_POST;
    		list($res['east'],$res['north']) = explode(",", $res['east']); 
    	}
    	/* $res['cityname'] = R('Reuse/cityname',array('',$res['cityid']));
    	$res['areaname'] = R('Reuse/cityname',array('','',$res['areaid'])); */
    	$this->assign('va',$res);
    	//城市
    	$list = M('region')->where("status = '0' and parent_id = '0'")->field("id,name")->order("id asc")->select();
    	$this->assign('list',$list); 
    	$this->display('Account/index');
    }
    /**
     * 保存商家基本信息
     */
    public function save_basc_shop(){
    	$db = D('Common/Shop');
    	if($create = $db->create()){
    		$uid = session('bxd_shop_user_id');
    		$create['id'] = $uid;
    		list($create['east'],$create['north']) = explode(",",$create['east']);
    		if(empty($create['north'])){
    			$this->index();
    			echo alert('经纬度填写错误',C('URL')."/Account/index");exit;
    		}
    		$res = $db->save($create);
    		if($res!=false){
    			$this->index();
    			echo alert('保存成功',C('URL')."/Account/index");
    		}else{
    			$this->index();
    			echo showmsg('你没有做任何的修改，无需保存编辑');
    		}
    	}else{
    		$this->index();
    		echo showmsg($db->getError());
    	}
    }
    /**
     * 认证信息
     */
    public function prove(){
    	$uid = session('bxd_shop_user_id');
    	$res = $this->admin_name($uid);
    	if(!empty($_POST)){
    		$res = $_POST;
    	}
    	$this->assign('va',$res);
    	//商家分类
    	$classlist = M('shop_classify')->order("sort desc")->field("id,name")->select();
    	$this->assign('classlist',$classlist);
    	$this->display('Account/prove');
    }
    /**
     * 保存认证信息
     */
    public function save_shop_prove(){
    	$uid = session('bxd_shop_user_id');
    	$db = D('Common/Shop');
    	$re = $db->where("id='{$uid}'")->find();
    	if(empty($re['shop_name'])){
    		$this->prove();
    		echo alert('检测到你还没有完善基础信息，无法进行认证',C('URL')."/Account/index");exit;
    	}
    	if($create = $db->create()){
    		//$uid = $_SESSION['eventor_shop_user_id'];
    		$res = $this->admin_name($uid);
    		if($res['is_check'] == 2){
    			$this->prove();
    			echo alert('你的资料已经认证啦，无需再次认证',C('URL')."/Account/prove");exit;
    		}
    		if($res['is_check'] == 0 && $res['bank']!=''){
    			$this->prove();
    			echo alert('你的资料已经提交啦，无需再次认证',C('URL')."/Account/prove");exit;
    		}
    		$create['id'] = $uid;
    		$create['is_check'] = 0;
    		$res = $db->save($create);
    		if($res!=false){
    			$this->prove();
    			echo alert('保存成功,请等待后台审核',C('URL')."/Account/prove");
    		}else{
    			$this->prove();
    			echo showmsg('你没有做任何的修改，无需保存编辑');
    		}
    	}else{
    		$this->prove();
    		echo showmsg($db->getError());
    	}
    }
    public function reset(){
    	$uid = session('bxd_shop_user_id');
    	$list = $this->admin_name($uid);
    	$this->assign('va',$list);
    	$this->display('Account/reset');
    }
    /**
     * 修改商户的密码
     */
    public function reset_pass(){
    	$uid = session('bxd_shop_user_id');
    	$list = $this->admin_name($uid);
    	$code = I('post.code');
    	$newpass = I('post.newpass');
    	$ckpass = I('post.ckpass');
    	$oldpass = I('post.oldpass');
    	if($newpass!=$ckpass){
    		$this->reset();
    		echo showmsg('两次密码输入不正确');exit;
    	}else{
    		$db = M('shop');
    		$a = \Org\Oa\msg::check_phone_code($list['username'],$code);
    		if($a){
    			$map['password'] = md5(C('MD5_PASS').$oldpass);
    			$map['id'] = $uid;
    			$res = $db->where($map)->find();
    			if(empty($res)){
    				$this->reset();
    				echo showmsg('旧密码输入错误');exit;
    			}
    			$data['password'] = $db->autopassword($newpass);
    			$data['id']  = $uid;
    			$re = $db->save($data);
    			if($re!=false){
    				M('phone_code')->where(array('phone'=>$list['username']))->setField('is_use',1);
    				$this->reset();
    				echo alert('密码修改成功',C('URL')."/Account/reset");exit;
    			}else{
    				$this->reset();
     				echo showmsg('新密码更旧密码一样，不需要修改');exit;
    			}
    		}else{
    			$this->reset();
    			echo showmsg('手机验证码输入错误');exit;
    		}
    	} 
    }
    /**
     * 发送短信验证码
     */
    public function sendmsg(){
    	$uid = session('bxd_shop_user_id');
    	$list = $this->admin_name($uid);
    	$a = \Org\Oa\msg::send_msg($list['username']);
    	if($a){
    		echo jsonencode(200,'发送短信成功');
    	}else{
    		echo jsonencode(300,'发送短信失败');
    	}
    }
}